package ym.controller.back.house;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.HouseImg;
import ym.service.HouseImgService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台房源图片
 */
@CrossOrigin
@RestController
@RequestMapping("/back/houseImg")
public class HouseImgCon {
    @Autowired
    private HouseImgService houseImgService;

    /**
     * 新增
     *
     * @param houseImg
     * @return 执行结果
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/house')")//springsecurity鉴权注解
    public BaseResult add(HouseImg houseImg) {
        houseImgService.add(houseImg);
        return BaseResult.ok();
    }


    /**
     * 修改
     *
     * @param houseImg
     * @return 执行结果
     */
    @PostMapping("/update")
    public BaseResult update(HouseImg houseImg) {
        houseImgService.update(houseImg);
        return BaseResult.ok();
    }


    /**
     * 删除
     *
     * @param houseImgId
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer houseImgId) {
        houseImgService.delete(houseImgId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询
     *
     * @param houseImgId
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer houseImgId) {
        HouseImg houseImg = houseImgService.findById(houseImgId);
        return BaseResult.ok(houseImg);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<HouseImg> all = houseImgService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询
     *
     * @param search 搜索条件
     * @param page   当前页
     * @param size   每页大小
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/house')")
    public BaseResult search(String search, int page, int size) {
        Page<HouseImg> houseImgPage = houseImgService.search(search, page, size);
        return BaseResult.ok(houseImgPage);
    }

    /**
     * 根据房源查询所有
     *
     * @param houseId
     * @return
     */
    @GetMapping("/findAllByHouseId")
    public BaseResult findAllByHouseId(Integer houseId) {
        List<HouseImg> all = houseImgService.findAllByHouseId(houseId);
        return BaseResult.ok(all);
    }

    /**
     * 根据房源分页查询
     *
     * @param houseId
     * @param search
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/searchByHouseId")
    @PreAuthorize("hasAnyAuthority('/house')")
    public BaseResult searchByHouseId(Integer houseId, String search, int page, int size) {
        Page<HouseImg> houseImgPage = houseImgService.searchByHouseId(houseId, search, page, size);
        return BaseResult.ok(houseImgPage);
    }
}
